﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MadeInPortugal;

namespace MadeInPortugalWebApplication
{
    public partial class EnterpriseUserWC : System.Web.UI.UserControl
    {
        int UserId = 0;
        String UserType;
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUser();

        }

        private void CheckUser()
        {
            if (!IsPostBack)
            {
                if (Session["UserType"] != null)
                    UserType = Session["UserType"].ToString();
                if (Session["UserId"] != null)
                {
                    int.TryParse(Session["UserId"].ToString(), out UserId);

                    if (UserId != 0)
                    {
                        Enterprise newEnterprise = new Enterprise();
                        newEnterprise = newEnterprise.Get(UserId);

                        TbxAddress.Text = newEnterprise.Address;
                        TbxCity.Text = newEnterprise.City;
                        TbxEmail.Text = newEnterprise.Email;
                        TbxEnterprise.Text = newEnterprise.EnterpriseName;
                        TbxName.Text = newEnterprise.Name;
                        TbxPhone.Text = newEnterprise.Phone;
                        TbxPostalCode.Text = newEnterprise.PostalCode;
                        newEnterprise.User = new EnterpriseUser();
                        newEnterprise.User = newEnterprise.User.getUserById(UserId);
                        TbxUserName.Text = newEnterprise.User.User;
                        TbxUserName.ReadOnly = true;
                        BtnSend1.Text = "<span>Update</span>";
                        imgLogo.ImageUrl = HttpContext.Current.Request.Url.Host.ToString() + "\\style\\img\\smallIcons\\" + UserId.ToString() + ".image";
                    }
                }
            }
        }
        protected void BtnSend_Click(object sender, EventArgs e)
        {
            Enterprise newEnterprise = new Enterprise();
            newEnterprise.Address = TbxAddress.Text;
            newEnterprise.City = TbxCity.Text;
            newEnterprise.Email = TbxEmail.Text;
            newEnterprise.EnterpriseName = TbxEnterprise.Text;
            newEnterprise.Name = TbxName.Text;
            newEnterprise.Phone = TbxPhone.Text;
            newEnterprise.PostalCode = TbxPostalCode.Text;
            newEnterprise.User = new EnterpriseUser();
            newEnterprise.User.User = TbxUserName.Text;
            if (BtnSend1.Text != "<span>Update</span>")
            newEnterprise.User.Password = TbxUserPassword.Text;
            newEnterprise.User.UserType = rdbUsertype.SelectedValue.ToString();

            if (FileLogoUpload.HasFile)
            {
                newEnterprise.Logo = new LogoImage();
                newEnterprise.Logo.Logo = FileLogoUpload.FileBytes;
                newEnterprise.Logo.Description = TbxEnterprise.Text;
            }
            if (BtnSend1.Text != "<span>Update</span>")
            { 
                newEnterprise.User = newEnterprise.User.getUserByName(TbxName.Text);
                if (newEnterprise.User.Id != 0)
                {
                    lblUserError.Enabled = true;
                    return;
                }
                else
                    lblUserError.Enabled = false;
                newEnterprise = newEnterprise.GetByEmail(TbxEmail.Text);
                if (newEnterprise.Id != 0)
                {
                    lblEmailError.Enabled = true;
                    return;
                }else
                    lblEmailError.Enabled = false;
               
                newEnterprise.Insert(newEnterprise);
            }
            else
            {
               

                newEnterprise.Id = int.Parse(Session["UserId"].ToString());
                newEnterprise.User.UserType = "Enterprise";
                newEnterprise.Update(newEnterprise);
            }

        }
    }
}